import pandas as pd
import numpy as np


def feature_engineering(data, logger=None):
    """
    1、在原有特征的基础上新增四个特征：
    相对薪资水平(IncomePerLevel)、成长停滞特征(PromotionGap)、岗位停滞情况(RoleGap)、工龄比率(ManagerRatio)
    2、对薪资水平和相对薪资水平进行对数化处理
    3、取出特征数据返回
    :param data: 数据源
    :param logger:日志
    :return:
    """
    # 2.1、将原数据进行拷贝
    data_df = data.copy()
    # 2.2、新增相对薪资水平(IncomePerLevel)、成长停滞特征(PromotionGap)、岗位停滞情况(RoleGap)、工龄比率(ManagerRatio)
    data_df['IncomePerLevel'] = data_df['MonthlyIncome'] / data_df['JobLevel']
    data_df['PromotionGap'] = data_df['YearsAtCompany'] - data_df['YearsSinceLastPromotion']
    data_df['RoleGap'] = data_df['YearsAtCompany'] - data_df['YearsInCurrentRole']
    data_df['ManagerRatio'] = data_df.apply(
        lambda x: x['YearsWithCurrManager'] / x['YearsAtCompany'] if x['YearsAtCompany'] != 0 else 0,
        axis=1
    )
    # 2.3、对工资和相对工资列进行对数化
    data_df['IncomePerLevel'] = np.log1p(data_df['IncomePerLevel'])
    data_df['MonthlyIncome'] = np.log1p(data_df['MonthlyIncome'])
    # 2.4、取出特征值和标签
    target_data = data_df.iloc[:, 0]
    feature_data = data_df.iloc[:, 1:]
    return feature_data, target_data


if __name__ == '__main__':
    data = pd.read_csv('../../data/processed/train.csv')
    feature_df, target_df = feature_engineering(data)

